package com.cn.wanxi.servlet;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.VO.TopVO;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.service.impl.CompanyServiceImpl;
import com.cn.wanxi.service.impl.NavServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/top")
public class TopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//Request请求对象：servlet的作用域？
// request ,指对请求页面有效
// session , 整个会话期有效，清楚浏览器缓存就没有了，跨浏览器就没有了
// application 整个会话期，可以跨浏览器
//        req.setAttribute("aa", "bb");
//        页面转发
//        req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);
//        String aa = "bb";
//        1乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2得到前端的值

//        3封装
//        4服务逻辑层
        CompanyService companyService = new CompanyServiceImpl();
        NavService navService = new NavServiceImpl();
//        TopService topService = new TopServiceImpl();
//        5得到数据
        CompanyModel companyModel = companyService.getCompanyModel();
        List<NavModel> navModelList = navService.getNavModelList();

//        6返回给前端
//        VO view Object 前端和后台页面传输对象  DTO
        TopVO topVO = new TopVO();
        topVO.setCompanyModel(companyModel);
        topVO.setNavModelList(navModelList);
        resp.getWriter().println(JSONObject.toJSONString(topVO));
//        resp.getWriter().println(JSONObject.toJSONString(navModelList));
    }
}
